<template>
  <div class="createPost-container">
    <el-form class="form-container" :model="postForm" :rules="rules" ref="postForm">
      <sticky :className="'sub-navbar '+ postForm.status">
        <template v-if="fetchSuccess">
          <el-button v-loading="loading" style="margin-left: 10px;" type="success" @click="submitForm()">发布
          </el-button>
          <el-button v-loading="loading" type="warning" @click="draftForm">草稿</el-button>
        </template>
        <template v-else>
          <el-tag>发生异常错误,刷新页面,或者联系程序员</el-tag>
        </template>
      </sticky>
      <div class="createPost-main-container">
        <el-row>
          <el-col :span="21">
            <el-form-item style="margin-bottom: 40px;" prop="title">
              <MDinput name="name" v-model="postForm.title" required :maxlength="100">
                标题
              </MDinput>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="editor-container">
          <tinymce :height=400 ref="editor" v-model="postForm.content"></tinymce>
        </div>
      </div>
    </el-form>

  </div>
</template>
<script>
import Tinymce from '@/components/Tinymce'
import MDinput from '@/components/MDinput'
import 'vue-multiselect/dist/vue-multiselect.min.css'// 多选框组件css
import Sticky from '@/components/Sticky' // 粘性header组件
import { publishExaminationRules, fetchExaminationRules } from '@/api/basicInfoSetting'

// const defaultForm = {
//   status: 'draft',
//   image_uri: '', // 文章图片
//   display_time: undefined, // 前台展示时间
//   id: undefined,
//   content: '',
//   title: ''
// }

export default {
  name: 'createArticle',
  components: { Tinymce, MDinput, Sticky },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        this.$message({
          message: rule.field + '为必传项',
          type: 'error'
        })
        callback(null)
      } else {
        callback()
      }
    }
    return {
      postForm: {
        title: '',
        content: '',
        status: 'draft'
      },
      fetchSuccess: true,
      loading: false,
      rules: {
        image_uri: [{ validator: validateRequire }]

      }
    }
  },
  computed: {
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      fetchExaminationRules().then(response => {
        // console.log(response)
        this.postForm.title = response.title
        this.postForm.content = response.content
      }).catch(error => {
        console.log(error)
        this.fetchSuccess = false
        this.$message.error('获取考试规则失败')
      })
    //   fetchArticle().then(response => {
    //     this.postForm = response.data
    //   }).catch(err => {
    //     this.fetchSuccess = false
    //     console.log(err)
    //   })
    },
    submitForm() {
      this.postForm.display_time = parseInt(this.display_time / 1000)
      console.log('postForm')
      console.log(this.postForm)
      this.$refs.postForm.validate(valid => {
        if (valid) {
          const param = {
            'title': this.postForm.title,
            'content': this.postForm.content
          }
          this.loading = true
          publishExaminationRules(param.title, param.content).then(response => {
            if (response.exception) {
              this.processing = false
              this.$message.error(response.exception.message)
              return
            }
            // this.$notify({
            //   title: '成功',
            //   message: '发布成功',
            //   type: 'success',
            //   duration: 2000
            // })
            this.postForm.status = 'published'
            this.loading = false
          }).catch(error => {
            console.log(error)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    draftForm() {
      if (this.postForm.content.length === 0) {
        this.$message({
          message: '请填写必要的标题和内容',
          type: 'warning'
        })
        return
      }
      this.$message({
        message: '保存成功',
        type: 'success',
        showClose: true,
        duration: 1000
      })
      this.postForm.status = 'draft'
    }
    // getRemoteUserList(query) {
    // //   userSearch(query).then(response => {
    // //     if (!response.data.items) return
    // //     console.log(response)
    // //     this.userLIstOptions = response.data.items.map(v => ({
    // //       key: v.name
    // //     }))
    // //   })
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/mixin.scss";
  .title-prompt{
    position: absolute;
    right: 0px;
    font-size: 12px;
    top:10px;
    color:#ff4949;
  }
  .createPost-container {
    position: relative;
    .createPost-main-container {
      padding: 40px 45px 20px 50px;
      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;
        .postInfo-container-item {
          float: left;
        }
      }
      .editor-container {
        min-height: 500px;
        margin: 0 0 30px;
        .editor-upload-btn-container {
            text-align: right;
            margin-right: 10px;
            .editor-upload-btn {
                display: inline-block;
            }
        }
      }
    }
    .word-counter {
      width: 40px;
      position: absolute;
      right: -10px;
      top: 0px;
    }
  }
</style>

